$(function(){
  // loop product\\\
  var intervalTimer = null;
  var currentDisplayIndex = 1;
  var linkElement = $('#big-image-link');
  var bigImageElement = $('#big-image');
  var images = _.map($('.big-image-src'),function(item){
    return [$(item).data('big'),$(item).data('ref')];
  });
  var totalImageCount = images.length;

  //init first image
  linkElement.attr("href",images[0][1]);
  bigImageElement.attr('src',images[0][0]);
  $('#big-image-link').imagefit({
    mode: 'outside',
    force : 'true',
    halign : 'left',
    valign : 'middle'
  });


  function _switchBigImage(){
    var imgObj = images[currentDisplayIndex];
    linkElement.attr('href',imgObj[1]);
    bigImageElement.fadeOut(400,function(){
      bigImageElement.attr('src',imgObj[0]).fadeIn();
    });
    currentDisplayIndex++;
    currentDisplayIndex = currentDisplayIndex % totalImageCount;
  }

  intervalTimer = setInterval(_switchBigImage,3000);

  $('.bike-name-link').click(function(){
    if(intervalTimer != null){
      clearInterval(intervalTimer);
      intervalTimer = null;
    }
    currentDisplayIndex = $('.prod-entry').index($(this).parents('.prod-entry'));
    _switchBigImage();

  });
});